<template >
  <div class="app-container">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"

            >导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-search"
              size="mini"
              @click="search"
            >搜索</el-button>
          </el-col>
           </el-row>
    <el-dialog :title="this.title" :visible.sync="open" width="1200px"  append-to-body>
      <el-form ref="form" :model="form"  label-width="120px" >
        <el-row>
          <el-col :span="12" v-if="form.id !==0">
            <el-form-item label="四色图名称" prop="name">
              <el-input v-model="form.name"  maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平面图类型" prop="type" maxlength="50">
              <el-select v-model="form.type" placeholder="未指定">
                <el-option
                  v-for="item in dataScopeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片" prop="photo">
              <el-upload
                class="upload-demo"
                action
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList"
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
      <el-table
        :data="tableData"
        style="width: 100%"
        height="500" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          fixed
          prop="name"
          label="平面图名称"
          width="200">
        </el-table-column>
        <el-table-column
          prop="type"
          label="平面图类型"
          width="200">
        </el-table-column>
        <el-table-column
          prop="photo"
          label="图片"
          width="200">

          <el-upload
            class="upload-demo"
            action
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture">
            <el-button size="small" type="primary" @click="uploadSuccess">点击上传</el-button>
              </el-upload>
        </el-table-column>
        <el-table-column
          prop="risk"
          label="风险分区"
          width="200">
        </el-table-column>
        <el-table-column
          prop="assessment"
          label="评估"
          width="100">

        </el-table-column>
        <el-table-column
          prop="history"
          label="评估历史"
          width="100">

        </el-table-column>
        <el-table-column
          prop="createTime"
          label="编辑时间"
          width="100">
        </el-table-column>
        <el-table-column
          label="操作"
          width="190">
        </el-table-column>
      </el-table>
  </div>
</template>
<script>
import {listpc, addPhoto, updatePhoto, delPhoto, getPhoto, uploadFile} from "@/api/system/pc";
import {getToken} from "@/utils/auth";

export default {
  name: "index",
  dicts: ['type'],
  data() {
    return {
        input:'',
        fileList: [],
        name:'',
        photo:'',
        title:'',
        ids:[],
        dataScopeOptions: [
        {
          value: "全厂",
          label: "全厂"
        },
        {
          value: "车间",
          label: "车间"
        }
      ],
        TypeOptions: undefined,
        form: {},
        open: false,
        tableData: [],
      //分页参数
        queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: '',

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/scyf/photo/importData"
      },



    }
  },
  //挂载
  created() {
    this.getList();
  },
  methods: {
    search() {
      this.title = "搜索"
    },
   //新增
    handleAdd() {
      this.open = true;
      this.title = "四色图详情"
    },
    //修改
    handleUpdate() {
     this.reset();
      getPhoto(this.ids[0]).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "四色图详情";
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection){
      this.ids = selection.map(item => item.id)
    },
    //查询
    getList() {
      //response形参（代表反回来的数据）
      listpc(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },
//删除
    handleDelete() {
      const id=this.ids;
      this.$modal.confirm(id).then(function() {
        return delPhoto(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePhoto(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPhoto(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
//图片上传
    uploadSuccess(item){
      let formData = new FormData()
      let file = item.raw
      formData.append('file', file)
      uploadFile(formData).then(ret=>{
        console.info("文件名："+JSON.stringify(ret));
        this.fileList[0] = ret.data;
        this.form.checkTable = ret.data.url;
        console.info("url"+this.form.checkTable);
      });
    },



    /** 下载模板操作 */
    importTemplate() {
      this.download('scyf/photo/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
  //导出
    handleExport() {
      this.download('scyf/photo/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {

      this.upload.title = "用户导入";
      this.upload.open = true;
      console.info(this.upload.open)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
      name: undefined,
        type: undefined,
      };
      this.resetForm("form");
    },
    //表里的按钮
    handleEdit(index, row) {

      console.log(index, row);
    },
    //表里的按钮
    handleEdit1(index, row) {
      console.log(index, row);
    },
    //表里的按钮
    handleEdit2(index, row) {
      console.log(index, row);
    },
  }
}
</script>
<style scoped>
</style>
